package com.xtwy.admin.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.xtwy.admin.web.mapper.RoleMapper;
import com.xtwy.admin.web.model.pojo.Role;
import com.xtwy.admin.web.service.RoleService;
import com.xtwy.common.utils.CamelCaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Set;

/**
 * 类的描述：
 *
 * @作者: 侯建军
 * @邮箱: 474177818@qq.com
 * @博客: https://blog.csdn.net/marquis888
 * @创建日期: 2018年11月18日  22:16
 * @版本: v1.0
 * @jdk版本使用: JDK1.8
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    /**
     * @param role
     * @return
     */
    @Override
    public List<Role> getPageList(Role role) {
//        PageHelper.offsetPage(role.getOffset(), role.getLimit());
        PageHelper.offsetPage(1, 10);
        String orderBy="updated_at desc";
        PageHelper.startPage(0, 10, orderBy);
//        PageHelper.orderBy(CamelCaseUtil.toUnderlineName(role.getSort()) + " " + role.getOrder());
        return roleMapper.selectAll();
    }

    /**
     * @return
     */
    @Override
    public List<Role> getFromAll() {
        Example example = new Example(Role.class);
        example.createCriteria()
                .andCondition("enable = ", 1);
        return roleMapper.selectByExample(example);
    }

    /**
     * @param example
     * @return
     */
    @Override
    public Integer getCount(Example example) {
        return roleMapper.selectCountByExample(example);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public Role getById(String id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    /**
     * @param roles
     * @return
     */
    @Override
    public List<Role> getById(Role roles) {
        return roleMapper.select(roles);
    }

    /**
     * @param id
     */
    @Override
    public void deleteById(String id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    /**
     * @param role
     */
    public void save(Role role) {
        roleMapper.updateByPrimaryKey(role);
    }

    /**
     * @param role
     */
    @Override
    public void insert(Role role) {
        roleMapper.insert(role);
    }

    /**
     * @param userId
     * @return
     */
    @Override
    public Set<String> findRoleByUserId(String userId) {
        return roleMapper.findRoleByUserId(userId);
    }

    /**
     * @param Id
     * @return
     */
    @Override
    public List<Role> selectRoleListByAdminId(String Id) {
        return roleMapper.selectRoleListByAdminId(Id);
    }
}
